import { defineStore } from 'pinia'

export const useFlow = defineStore('flow', {
    state: () => ({
        isTried: false,
        approverDrawer: false,
        approverConfig1: {},
        conditionDrawer: false,
        conditionsConfig1: {
            conditionNodes: []
        },
        nodeConfig1: {}
    }),
    actions: {
        setIsTried(payload: any) {
            this.isTried = payload
        },
        setApprover(payload: any) {
            this.approverDrawer = payload
        },
        setApproverConfig(payload: any) {
            this.approverConfig1 = payload
        },
        setCondition(payload: any) {
            this.conditionDrawer = payload
        },
        setConditionsConfig(payload: any) {
            this.conditionsConfig1 = payload
        },
        setNodeConfig(payload: any) {
            this.nodeConfig1 = payload
        },
        uuidFn() {
            return `node_${new Date().getTime().toString().substring(5)}${Math.round(
                Math.random() * 9000 + 1000
            )}`
        },
        addNodeIdAndFatherId(node: any) {
            if (node.childNode) {
                node.childNode.nodeId = node.childNode.nodeId
                    ? node.childNode.nodeId
                    : this.uuidFn()
                node.childNode.fatherId = node.nodeId || 1
                this.addNodeIdAndFatherId(node.childNode)
            }
            if (node.conditionNodes) {
                node.conditionNodes.forEach((conditionNode: any) => {
                    conditionNode.nodeId = conditionNode.nodeId
                        ? conditionNode.nodeId
                        : this.uuidFn()
                    conditionNode.fatherId = node.nodeId
                    this.addNodeIdAndFatherId(conditionNode)
                })
            }
        }
    }
})
